package com.qs.education.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.qs.education.bean.api.PageBean;
import com.qs.education.bean.api.ResponseBean;
import com.qs.education.bean.api.ResponseListBean;
import com.qs.education.bean.beans.EducationGradeBean;
import com.qs.education.bean.beans.EducationSubjectBean;
import com.qs.education.bean.entity.EducationQuestion;
import com.qs.education.bean.entity.EducationSMS;
import com.qs.education.bean.entity.EducationSubjectUnit;
import com.qs.education.bean.enums.ResponseStatus;
import com.qs.education.service.IGradeService;
import com.qs.education.service.IQuestionService;
import com.qs.education.service.ISubjectService;
import com.qs.education.service.ISubjectUnitService;

/**
 * @class QuestionController
 * @author Jason
 * @description
 * @date Nov 22, 2018 10:19:06 AM
 */
@Controller
public class QuestionController {

	@Autowired
	private IQuestionService iQuestionService;
	@Autowired
	private IGradeService iGradeService;
	@Autowired
	private ISubjectService iSubjectService;
	@Autowired
	private ISubjectUnitService iSubjectUnitService;
	
	
	private final Logger log = Logger.getLogger(this.getClass());
	
	/**
	 * @Description  查询所有符合条件的题目信息
	 * @author Jason
	 * @date Nov 22, 2018
	 * @param sms
	 * @param currentPage
	 * @param pageSize
	 * @return
	 */
	@RequiresPermissions(value={"user:view"})
	@RequestMapping("/getQuestion")
	public ModelAndView getQuestion(EducationQuestion question,
			@RequestParam(defaultValue="1",value="currentPage") Integer currentPage,
			@RequestParam(defaultValue="5",value="pageSize") Integer pageSize){
		
		log.info("后台管理--> 查询所有符合条件的题目信息");
		ModelAndView mv=new ModelAndView("question/list");
		PageBean questionPageBean =  iQuestionService.getQuestionList(question,currentPage,pageSize);
		ResponseListBean<EducationGradeBean> gradeList = iGradeService.getGradeList();
		ResponseListBean<EducationSubjectBean> subjectList = iSubjectService.getSubjectList();
		ResponseListBean<EducationSubjectUnit> unitList =new ResponseListBean<>();
		if (question.getLesson()!=null&&question.getLesson().getUnit()!=null) {
			EducationSubjectUnit unit = new EducationSubjectUnit();
			unit.setGrade(question.getLesson().getUnit().getGrade());
			unit.setSubject(question.getLesson().getUnit().getSubject());
			unitList = iSubjectUnitService.getUnitList(unit);
			
			/*if(unitList!=null&&unitList.getList()!=null) {
				List<EducationSubjectUnit> list = (List<EducationSubjectUnit>)unitList.getList();
				for(EducationSubjectUnit educationUnit :list) {
					if (educationUnit.getId() == question.getLesson().getUnit().getId()) {
						List<EducationSubjectLesson> lessonList = educationUnit.getSubjectLessonList();
						mv.addObject("lessonList", lessonList);
					}
				}
			}*/
		}
		
		mv.addObject("questionPageBean", questionPageBean);
		mv.addObject("gradeList", gradeList);
		mv.addObject("subjectList", subjectList);
		mv.addObject("unitList", unitList);
		mv.addObject("question", question);
		
		return mv;
	}
	
	/**
	 * @Description 根据ID删除Question
	 * @author Jason
	 * @date Nov 23, 2018
	 * @param sms
	 * @param currentPage
	 * @param pageSize
	 * @param attr
	 * @return
	 */
	@RequiresPermissions(value={"user:delete"})
	@RequestMapping("/deleteQuestion")
	public ModelAndView deleteQuestionById(EducationQuestion question,
			@RequestParam(defaultValue="1",value="currentPage") Integer currentPage,
			@RequestParam(defaultValue="5",value="pageSize") Integer pageSize){
		
		log.info("后台管理-->删除Question");
		ModelAndView mv=new ModelAndView("forward:/getQuestion");
		ResponseBean response =  iQuestionService.deleteQuestionById(question.getId());
		
		return mv;
	}
	
	@RequiresPermissions(value={"user:view"})
	@RequestMapping("/uploadQuestionExcelFileHTML")
	public ModelAndView uploadQuestionExcelFileHTML(){
		
		log.info("后台管理-->跳转上传题库EXCEL文件页面");
		ModelAndView mv=new ModelAndView("/question/uploadQuestionExcelFile");
		
		return mv;
	}
	
	@RequiresPermissions(value={"user:add"})
	@RequestMapping("/uploadQuestionExcelFile")
	@ResponseBody
	public Map<String, Object> uploadQuestionExcelFile(HttpServletRequest request,
			@RequestParam(value = "questionExcel", required = false) MultipartFile file){
		
		log.info("后台管理-->上传题库EXCEL文件");
		Map<String, Object> response = iQuestionService.uploadQuestionExcelFile(request,file);
		List<EducationQuestion> list = (List<EducationQuestion>) response.get("list");
		request.setAttribute("list",list);
		
		return response;
	}
	
}
